package cnsukidayo.com.gitee;

import cnsukidayo.com.gitee.utils.Resources;
import com.google.gson.Gson;
import org.fisco.bcos.sdk.BcosSDK;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.eventsub.EventCallback;
import org.fisco.bcos.sdk.eventsub.EventLogParams;
import org.fisco.bcos.sdk.eventsub.EventSubscribe;
import org.fisco.bcos.sdk.eventsub.EventSubscribeImp;
import org.fisco.bcos.sdk.model.EventLog;

import java.util.ArrayList;
import java.util.List;

public class BcosSDKTest4 {

    private Gson gson = new Gson();
    // 获取配置文件路径
    public final String configFile = Resources.getResource("config.toml");

    public void testClient() throws Exception {
        // 初始化BcosSDK
        BcosSDK bcosSDK = BcosSDK.build(configFile);
        Client client = bcosSDK.getClient(1);
        CryptoKeyPair keyPair = client.getCryptoSuite().createKeyPair();

        // 其他初始化逻辑，省略

        EventSubscribe eventSubscribe = new EventSubscribeImp(client.getGroupManagerService(), client.getEventResource(), client.getGroupId());
        // 参数设置
        EventLogParams params = new EventLogParams();

        // 全部Event fromBlock设置为"1"
        params.setFromBlock("1");

        // toBlock设置为"latest"，处理至最新区块继续等待新的区块
        params.setToBlock("latest");

        // addresses设置为空数组，匹配所有的合约地址
        params.setAddresses(new ArrayList<String>());

        // topics设置为空数组，匹配所有的Event
        params.setTopics(new ArrayList<Object>());

        String registerId = eventSubscribe.subscribeEvent(params, new EventCallback() {
            @Override
            public void onReceiveLog(int status, List<EventLog> logs) {
                System.out.println("触发事件!!!");
            }
        });
        System.out.println(registerId);

    }

    public static void main(String[] args) throws Exception {
        BcosSDKTest4 bcosSDKTest4 = new BcosSDKTest4();
        bcosSDKTest4.testClient();
    }

}
